--
-- Author: shunguo.chen
-- Date: 2016-10-20 15:51:48
--
-- RewardOnlineVo

local Crontab = require("app.utils.Crontab")

local fileName = string.getModeName(...)
local ClassRef = class(fileName)

-- data = {
--	 id=,	 活动id，也可以读取
-- }
function ClassRef:ctor(data)
	self.id = data.id

	local conf = ZD:queryActivityById(self.id)
	if conf == {} then return end
	-- dump(conf, "RewardOnlineVo.conf")

	self.rewards = {}
	local ary = string.split(conf.newrewardIcon, ";")
	for i,v in ipairs(ary) do
		local reward = UD:parseRewardToBagItem(v)
		if reward then
			self.rewards[#self.rewards + 1] = reward
		end
	end

	self._refreshGap = 0

	if conf.startTime1 and conf.endTime1 then
		self._hasReward = false
		self._startCron = Crontab.new(conf.startTime1)
		self._endCron = Crontab.new(conf.endTime1)
		self:refreshHasReward()
	else
		self._hasReward = true
	end
end

function ClassRef:refreshHasReward()
	if not self._startCron or not self._endCron then return end
	local serverTime = app.session:getServerTime(true) or os.time()
	serverTime = math.floor(serverTime)

	if not self._endScd or (serverTime > self._endScd) then
		self._startScd = self._startCron:next(serverTime)
		self._endScd = self._endCron:next(serverTime)

		if self._startScd > self._endScd then
			self._startScd = self._startCron:previous(serverTime)
		end
	end

	self._hasReward = false
	self._refreshGap = 0
	if serverTime < self._startScd then
		self._refreshGap = self._startScd - serverTime
	else
		self._refreshGap = self._endScd - serverTime
	end

	-- print("RewardOnlineVo.serverTime", serverTime)
	-- print("RewardOnlineVo.serverTime", serverTime)
	-- print("RewardOnlineVo.lastTime", lastTime)
	-- print("RewardOnlineVo._hasReward", self._hasReward)
	-- print("RewardOnlineVo._refreshGap", self._refreshGap)
	-- print("RewardOnlineVo.serverTime", self._startScd)
	-- print("RewardOnlineVo.serverTime", self._endScd)
end

function ClassRef:setHasReward(b)
	self._hasReward = b
end
function ClassRef:hasReward()
	return self._hasReward
end

function ClassRef:getRefreshGap()
	return self._refreshGap
end

return ClassRef
